package cn.ljy.uem.service.impl;

import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.common.utils.page.Pagination;
import cn.ljy.uem.dao.SchoolTeacherMapper;
import cn.ljy.uem.dao.SchoolTeachingTaskMapper;
import cn.ljy.uem.dao.SchoolTeachingTaskTeacherMapper;
import cn.ljy.uem.model.*;
import cn.ljy.uem.model.query.SchoolTeachingTaskTeacherQuery;
import cn.ljy.uem.service.SchoolTeachingTaskTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.*;


/**
 * @description 教学任务教师模块的业务层接口
 * @author 廖振豪
 * @date 2022-7-21
 */
@Service
@Slf4j
public class ISchoolTeachingTaskTeacherService implements SchoolTeachingTaskTeacherService {

    @Autowired
    private SchoolTeachingTaskTeacherMapper mapper;

    @Autowired
    private SchoolTeacherMapper schoolTeacherMapper;

    @Autowired
    private SchoolTeachingTaskMapper schoolTeachingTaskMapper;

    @Override
    public SchoolTeachingTaskTeacher get(Serializable teachingTaskId, Serializable teacherId) throws MyWebException {
        SchoolTeachingTaskTeacher result=null;
        //适合读取数据前的验证
        this.beforeGet(teachingTaskId, teacherId);
        //从数据库读取记录
        result=mapper.findByTeachingTaskIdAndTeacherId(teachingTaskId, teacherId);
        //读取后的数据处理，例如字段修改，添加关联属性
        if(result!=null){
            this.afterGet(result);
        }
        return result;
    }

    //    @Override
    public void save(@NotNull(message="传递参数不能为null") @Valid SchoolTeachingTaskTeacher teacher) {
        //适合保存数据前的验证
        beforeSave(teacher);
        //保存数据后的处理
        //将前端传递过来的集合转换为set集合,顺便去重
        Set<Long> teacherIdsSet = new HashSet<Long>(Arrays.asList(teacher.getTeacherIds()));
        //获取原来的教师
        List<SchoolTeacher> teachersOrigin=mapper.findAllTeacherByTeachingTaskId(teacher.getTeachingTaskId());
        //处理教学任务和教师的关系
        for(Long teacherId:teacherIdsSet){
            if(teachersOrigin.contains(teacherId)){
                throw new MyWebException(schoolTeacherMapper.findById(teacherId).getName() + "已经存在");
            }
        }
        for (Long teacherId:teacherIdsSet){
            if (teacher.getType() != null){
                mapper.save(teacher.getTeachingTaskId(),teacherId,teacher.getType());
            }else{
                teacher.setType(2);
                mapper.save(teacher.getTeachingTaskId(),teacherId,teacher.getType());
            }

        }

        afterSave(teacher);
    }

    @Override
    public void update(@NotNull(message="传递参数不能为null") @Valid SchoolTeachingTaskTeacher teacher) {
        //适合更新数据前的验证
        beforeUpdate(teacher);
        mapper.update(teacher);
        //更新数据后的处理
        afterUpdate(teacher);
    }


    @Override
    public void delete(@NotNull(message="传递参数不能为null") Integer teachingTaskId,@NotEmpty(message = "操作失败：请选择要移除的记录") Serializable[] teacherIds) {
        beforeDelete(teachingTaskId,teacherIds);
        for (Serializable id : teacherIds) {
            mapper.delete(teachingTaskId,id);
        }
        afterDelete(teachingTaskId,teacherIds);
    }

    @Override
    public List<SchoolTeachingTaskTeacher> findAll(SchoolTeachingTaskTeacherQuery query) {
        List<SchoolTeachingTaskTeacher> list=new ArrayList<>();
        beforeFindAll(query);
        list=mapper.findAll(query);
        afterFindAll(list);
        return list;
    }

    @Override
    public Pagination findAll(Integer page, Integer pageSize, SchoolTeachingTaskTeacherQuery query) {
        beforeFindAll(query);
        //验证并计算相关分页数据
        Pagination pagination = new Pagination(page, pageSize,mapper.count(query));
        //为当前页的数据集赋值
        pagination.setData(mapper.find(pagination.getOffset(),pagination.getPageSize(),query));
        //对账户信息进行相应处理

        afterFindAll(pagination.getData());
        return pagination;
    }





    protected void beforeGet(Serializable teachingTaskId, Serializable teacherId) {
    }
    protected void afterGet(SchoolTeachingTaskTeacher teacher) {
    }

    protected void beforeUpdate(SchoolTeachingTaskTeacher schoolTeachingTaskTeacher) {

    }

    protected void afterUpdate(SchoolTeachingTaskTeacher schoolTeachingTaskTeacher){}

    //在添加函数之前执行
    protected void beforeSave(SchoolTeachingTaskTeacher schoolTeachingTaskTeacher){};

    //在添加函数之后执行
    protected void afterSave(SchoolTeachingTaskTeacher schoolTeachingTaskTeacher){}

    protected  void beforeDelete(Serializable teachingTaskId,Serializable[] ids){

    };

    protected void afterDelete(Serializable teachingTaskId,Serializable[] ids){}



    protected void beforeFindAll(SchoolTeachingTaskTeacherQuery query) {
    }
    //教学班教师多表查询
    protected void afterFindAll(List<SchoolTeachingTaskTeacher> list) {
        for (SchoolTeachingTaskTeacher schoolTeachingClassTeacher:list){
            if(schoolTeachingClassTeacher.getTeacherId()!=null){
                SchoolTeacher schoolTeacher= schoolTeacherMapper.findById(schoolTeachingClassTeacher.getTeacherId());
                schoolTeachingClassTeacher.setSchoolTeacher(schoolTeacher);
            }
            if(schoolTeachingClassTeacher.getTeachingTaskId()!=null){
                SchoolTeachingTask teacherTask = schoolTeachingTaskMapper.findById(schoolTeachingClassTeacher.getTeachingTaskId());
                schoolTeachingClassTeacher.setSchoolTeachingTask(teacherTask);
            }
        }
    }

}
